package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.PalmScanResultAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.HandBean
import com.horoscope.astrology.zodiac.signs.free.bean.HandBeanData
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.*
import kotlinx.android.synthetic.main.activity_zodiac_result.*
import kotlinx.android.synthetic.main.view_network_error.*

class PalmScanResultActivity : BaseActivity() {

    private var mAdapter: PalmScanResultAdapter? = null
    private var mList = mutableListOf<HandBeanData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_palm_scan_result)


        for (i in 0..2) {
            val bean = HandBeanData()
            bean.number = (0..100).random().toString()
            mList.add(bean)
        }
        initView()
//        request()
//        activityShowAd("Interstitial_Hand")
    }

    private fun request() {
        showLoading()
        NetworkEngine.getPalmScanResult("", "", listener)
    }

    private val listener = object : NetworkListener<HandBean>() {
        override fun onSuccess(t: HandBean) {
            showLoaded()
            include_error.visibility = View.GONE
        }

        override fun onFailed(t: HandBean) {
            super.onFailed(t)
            errorMake()
        }

        override fun onError(e: String?) {
            super.onError(e)
            errorMake()
        }

    }

    private fun errorMake() {
        showLoaded()
        include_error.visibility = View.VISIBLE
    }

    private fun initView() {
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            "Palm Scan"
        )

        mRecyclerView = findViewById(R.id.rv_palm_scan)
        mAdapter = PalmScanResultAdapter(this)
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        mAdapter!!.setData(mList)
        mRecyclerView.addItemDecoration(PalmScanDecoration(30f, 30f))
        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            request()
        }
    }
}
